package com.sgrape.appinstaller;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    TextView textView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textView);
    }

    @Override
    public void onClick(View v) {
        InputStream in = null;
        ByteArrayOutputStream baos = null;
        try {
            in = getAssets().open("test.txt");
            baos = new ByteArrayOutputStream();
            int len;
            byte[] buf = new byte[128];
            while ((len = in.read(buf)) > 0) {
                baos.write(buf, 0, len);
                baos.flush();
            }
            appendTxt(baos.toString());
        } catch (IOException e) {
            appendTxt(e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                if (in != null) in.close();
                if (baos != null) baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void appendTxt(String txt) {
        textView.append("\n" + txt);
    }
}
